#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, k, q;
int len[N];
vector<int> s[N];

int ans[105][N];
// -1 可以从任意人开始接龙
// 1 - n 不能从指定人开始接龙
// 0 不能接龙
void solve() {
  memset(ans, 0, sizeof(ans));
  cin >> n >> k >> q;
  rep(i, 1, n) s[i].clear();
  rep(i, 1, n) {
    cin >> len[i];
    rep(j, 1, len[i]) {
      int x;
      cin >> x;
      s[i].push_back(x);
    }
  }
  ans[0][1] = -1;
  rep(r, 1, 100) {
    // 遍历所有人的序列
    rep(i, 1, n) {
      int right = -1;
      rep(j, 0, len[i] - 1) {
        int v = s[i][j];
        if (j <= right) {  // 可以作为本轮的接龙
          if (ans[r][v] == 0 || ans[r][v] == i)
            ans[r][v] = i;
          else
            ans[r][v] = -1;
        }
        if (ans[r - 1][v] && ans[r - 1][v] != i) {  // 可以作为上轮的接龙
          right = min(j + k - 1, len[i] - 1);
        }
      }
    }
  }
  while (q--) {
    int r, x;
    cin >> r >> x;
    if (ans[r][x])
      cout << 1 << endl;
    else
      cout << 0 << endl;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}